﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace SportManager.TrainingWizard
{
    public class SlideB : SlickInterface.Slide
    {
        private Label label1;
        private ListBox listBox1;
        private Button button2;
        private Button button3;
        private RichTextBox richTextBox1;
        private Label label2;
        private Label label3;
        private Training m_training;

        public SlideB(ref Training training)
        {
            m_training = training;
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(259, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(40, 13);
            this.label1.TabIndex = 7;
            this.label1.Text = "Slide B";
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(12, 75);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(240, 199);
            this.listBox1.TabIndex = 10;
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(164, 51);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(88, 23);
            this.button2.TabIndex = 9;
            this.button2.Text = "Удалить";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(12, 51);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(93, 23);
            this.button3.TabIndex = 8;
            this.button3.Text = "Добавить";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(274, 75);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.Size = new System.Drawing.Size(240, 199);
            this.richTextBox1.TabIndex = 13;
            this.richTextBox1.Text = "";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(271, 56);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(78, 13);
            this.label2.TabIndex = 11;
            this.label2.Text = "Группа мышц:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(349, 56);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(0, 13);
            this.label3.TabIndex = 12;
            // 
            // SlideB
            // 
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.label1);
            this.Name = "SlideB";
            this.Size = new System.Drawing.Size(530, 300);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.SlideControl.ForwardTo<SlideC>();
        }        

        private void button3_Click(object sender, EventArgs e)
        {
            List<ExerciseInfo> infos = new List<ExerciseInfo>();
            foreach (Exercise item in m_training.Exercises)
            {
                infos.Add(Program.ExercisesKeeper.FindItemByGuid(item.Id));
            }

            ExercisesListDlg dlg = new ExercisesListDlg(true, infos);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                m_training.Exercises = ExerciseMerger.MergeLists(dlg.SelectedInfos, m_training.Exercises);
            }
            AddTrainingWizard form = (AddTrainingWizard)this.Parent.Parent;
            if (m_training.Exercises.Count > 0)
            {
                form.button2.Enabled = true;
            }

            ShowExercises();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Вы уверены ?", "Удаление", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    m_training.Exercises.RemoveAt(listBox1.SelectedIndex);
                    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                    if (listBox1.Items.Count == 0)
                    {
                        AddTrainingWizard form = (AddTrainingWizard)this.Parent.Parent;
                        form.button2.Enabled = false;
                    }
                }
            }
            else
            {
                MessageBox.Show("Не выбрано ни одно из упражнений", "Ошибка", MessageBoxButtons.OK);
            }
        }

        private void ShowExercises()
        {
            listBox1.Items.Clear();
            for (int i = 0; i < m_training.Exercises.Count; i++)
            {
                listBox1.Items.Add(Program.ExercisesKeeper.FindItemByGuid(m_training.Exercises[i].Id).Name);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                label3.Text = Program.ExercisesKeeper.FindItemByGuid(m_training.Exercises[listBox1.SelectedIndex].Id).GetLoadsListString();
                richTextBox1.Text = Program.ExercisesKeeper.FindItemByGuid(m_training.Exercises[listBox1.SelectedIndex].Id).Description;
            }
            else
            {
                label3.Text = "";
                richTextBox1.Text = "";
            }
        }
    }
}
